<template>
  <div>
    <el-date-picker v-model="date" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"
      style="margin: 20px 10% 0;" @change="dateChange" :picker-options="pickerOptions">
    </el-date-picker>
    <el-button type="primary" style="margin: 20px 10% 0;"
      @click="$router.push({ name: 'print', params: { start: formDate[0], end: formDate[1] } })">
      打印工资单
    </el-button>
    <el-button type="primary" style="margin: 20px 10% 0;" @click="punchCard = true">
      添加打卡记录
    </el-button>
    <el-table :data="cardList" stripe border style="width: 80%;margin: 20px auto;">
      <el-table-column prop="project_name" label="项目名"></el-table-column>
      <el-table-column prop="date" label="时间"></el-table-column>
      <el-table-column prop="location" label="地点"></el-table-column>
      <el-table-column prop="days" label="工天"></el-table-column>
      <el-table-column prop="square_meter" label="平方米"></el-table-column>
      <el-table-column prop="type" label="类型"></el-table-column>
      <el-table-column prop="user_name" label="姓名"></el-table-column>
      <el-table-column prop="admin_name" label="审核人"></el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.admin_approved == 0" type="info">
            未审核
          </el-tag>
          <el-tag v-if="scope.row.admin_approved == 1" type="warning">
            已审核
          </el-tag>
          <el-tag v-if="scope.row.admin_approved == 2" type="success">
            已计入
          </el-tag>
          <el-tag v-if="scope.row.admin_approved == 3" type="danger">
            已驳回
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="350">
        <template slot-scope="scope">
          <el-button type="primary" @click="openPass(scope.row)">通过
          </el-button>
          <el-button type="danger" @click="openReject(scope.row)">驳回
          </el-button>
          <el-button type="primary" @click="openAdmin(scope.row)">查看
          </el-button>
          <el-button type="primary" @click="openEditAdmin(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="确认驳回" :visible.sync="rejectVisible" width="30%">
      <span>你确认驳回该打卡记录吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rejectVisible = false">取 消</el-button>
        <el-button type="primary" @click="reject">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="确认通过" :visible.sync="passVisible" width="30%">
      <span>你确认通过该打卡记录吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="passVisible = false">取 消</el-button>
        <el-button type="primary" @click="pass">确 定</el-button>
      </span>
    </el-dialog>

    <!--    管理员审核打卡拍照-->
    <el-dialog title="管理员拍照审核" :visible.sync="adminModel" :width="width > 800 ? '60%' : '100%'">
      <div>
        <div v-for="item in audit" v-if="audit.length > 0">
          <p>时间：{{ item.time }} 上传</p>
          <p>地点：{{ item.place }}</p>
          <viewer :images="item.image.split(';')">
            <div v-for=" item3  in  item.image.split(';')">
              <img :src="item3" style="width: 100%" alt="">
            </div>
          </viewer>
        </div>
        <div v-if="audit.length === 0">
          该审核人未审核上传
        </div>
      </div>
    </el-dialog>

    <el-dialog title="添加打卡记录" :visible.sync="punchCard">
      <el-form :model="punchCardForm" ref="punchCardForm" :rules="rules">
        <el-form-item label="用户" prop="user_id">
          <el-select v-model="punchCardForm.user_id" placeholder="请选择用户">
            <el-option v-for="item in users" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间" prop="date">
          <el-date-picker v-model="punchCardForm.date" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="项目" prop="project_id">
          <el-select v-model="punchCardForm.project_id" placeholder="请选择项目" @change="getTypes">
            <el-option v-for="item in projects" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="punchCardForm.type" placeholder="请选择工序" :disabled="!punchCardForm.project_id">
            <el-option v-for="item in types" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地区" prop="location">
          <el-input v-model="punchCardForm.location"></el-input>
        </el-form-item>
        <el-form-item label="工天" prop="days">
          <el-select clearable v-model="punchCardForm.days" placeholder="请选择工天" :disabled="!!punchCardForm.square_meter">
            <el-option label="半天" value="0.5"></el-option>
            <el-option label="一天" value="1"></el-option>
            <el-option label="加班天" value="1.5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平方米" prop="square_meter">
          <el-input v-model.number="punchCardForm.square_meter" :disabled="!!punchCardForm.days"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="punchCard = false">取 消</el-button>
        <el-button type="primary" @click="addPunchCard('punchCardForm')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑打卡记录" :visible.sync="editCard">
      <el-form :model="punchCardForm" ref="punchCardForm" :rules="rules">
        <el-form-item label="时间" prop="date">
          <el-date-picker v-model="punchCardForm.date" type="date" placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="punchCardForm.type" placeholder="请选择工序" :disabled="!punchCardForm.project_id">
            <el-option v-for="item in types" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工天" prop="days">
          <el-select clearable v-model="punchCardForm.days" placeholder="请选择工天" :disabled="!!punchCardForm.square_meter">
            <el-option label="半天" value="0.5"></el-option>
            <el-option label="一天" value="1"></el-option>
            <el-option label="加班天" value="1.5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平方米" prop="square_meter">
          <el-input v-model.number="punchCardForm.square_meter" :disabled="!!punchCardForm.days"></el-input>
        </el-form-item>
        <el-form-item label="当天工钱" prop="money">
          <el-input v-model.money="punchCardForm.money"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editCard = false">取 消</el-button>
        <el-button type="primary" @click="ConfirmEditCard">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { api_getPunchCard, api_audit } from '@/api'
import user from '@/store/user';

export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (!this.punchCardForm.square_meter && !this.punchCardForm.days) {
        callback(new Error('必须填入工天或平方米'));
      }
      callback();
    };
    return {
      punchCardForm: {
        project_id: '',
        type: '',
        location: '',
        days: '',
        square_meter: '',
        user_id: '',
        date: '',
        money: '',
        id: '',
      },
      editCardFrom: {},
      projects: [],
      types: [],
      users: [],
      cardList: [],
      approvedInfo: {},
      rejectVisible: false,
      passVisible: false,
      adminModel: false,
      width: innerWidth,
      audit: [],
      date: [],
      pickerOptions: {
        disabledDate(time) {
          const today = new Date(); // 获取当前日期
          return time.getTime() > today.getTime(); // 将结束日期大于当前日期的日期禁用
        }
      },
      punchCard: false,
      editCard: false,

      rules: {
        project_id: [
          { required: true, message: '请选择项目', trigger: 'change' }
        ],
        type: [
          { required: true, message: '请选择工序', trigger: 'change' }
        ],
        user_id: [
          { required: true, message: '请选择用户', trigger: 'change' }
        ],
        date: [
          { required: true, message: '请选择时间', trigger: 'change' }
        ],
        days: [
          { validator: validatePass }
        ],
        square_meter: [
          { validator: validatePass }
        ],


      }
    }
  },
  methods: {
    getUsers() {
      this.$http.get('/admin/api/getUsers').then(res => {
        this.users = res.data.data
      })
    },
    getProjects() {
      this.$http.get('/client/api/getProject').then(res => {
        this.projects = res.data.data
      })
    },
    getTypes() {
      this.$http.get('/client/api/getProjectType?id=' + this.punchCardForm.project_id).then(res => {
        this.types = res.data.data
      })
    },
    addPunchCard(formName) {
      console.log('addPunchCard', this.$refs[formName])
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.punchCard = false;
          this.$http.post('/admin/auth/addWokerSignIn', this.punchCardForm).then(res => {
            if (res.data.status === 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.dateChange()
            } else {
              this.$message({
                type: 'error',
                message: res.data.message
              })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });

      // this.$refs.punchCardForm.validate((valid) => {
      //       console.log(valid,111111111111111)
      //       if (valid) {
      //         this.punchCard = false;
      //         this.$http.post('/admin/auth/addWokerSignIn', this.punchCardForm).then(res => {
      //           if (res.data.status === 200) {
      //             this.$message({
      //               type: 'success',
      //               message: '添加成功'
      //             })
      //             this.dateChange()
      //           } else {
      //             this.$message({
      //               type: 'error',
      //               message: res.data.message
      //             })
      //           }
      //         })
      //       }
      //     })


    },
    dateChange() {
      api_getPunchCard({
        start: this.formDate[0],
        end: this.formDate[1]
      }).then(res => {
        if (res.status == 200) {
          this.cardList = res.data
        } else {
          this.$message({
            type: 'error',
            message: res.message
          })
        }
      })
    },
    openPass(data) {
      this.passVisible = true
      this.approvedInfo = data
    },
    openReject(data) {
      this.approvedInfo = data
      this.rejectVisible = true
    },
    openAdmin(data) {
      this.approvedInfo = data
      this.adminModel = true

      console.log(11111111111, data)

      this.$http.get('/admin/auth/getAdminRecord', {
        params: {
          id: data.admin_id,
          date: data.date.split(' ')[0],
        }
      }).then(res => {
        console.log(res.data.data)
        this.audit = res.data.data
      })


    },
    pass() {
      api_audit({
        id: this.approvedInfo.id,
        status: 2
      }).then(res => {
        if (res.status == 200) {
          this.passVisible = false
          api_getPunchCard({
            start: this.formDate[0],
            end: this.formDate[1]
          }).then(res => {
            console.log(this.cardList)
            this.cardList = res.data
          })
          this.$message({
            type: 'success',
            message: '该打卡已通过'
          })
        } else {
          this.$message({
            type: 'error',
            message: res.message
          })
        }
      })
    },
    reject() {
      api_audit({
        id: this.approvedInfo.id,
        status: 3
      }).then(res => {
        this.rejectVisible = false
        api_getPunchCard({
          start: this.formDate[0],
          end: this.formDate[1]
        }).then(res => {
          this.cardList = res.data
        })
        if (res.status == 200) {
          this.$message({
            type: 'success',
            message: '该打卡已驳回'
          })
        } else {
          this.$message({
            type: 'error',
            message: res.message
          })
        }
      })
    },
    openEditAdmin(data) {
      console.log(data)
      this.punchCardForm = data
      this.editCard = true
    },
    ConfirmEditCard() {
      console.log(this.punchCardForm)
      this.$http.post(`admin/auth/updatePunchCard`, {
        id: this.punchCardForm.id,
        type: this.punchCardForm.type,
        days: this.punchCardForm.days,
        date: this.punchCardForm.date,
        square_meter: this.punchCardForm.square_meter,
        money: this.punchCardForm.money,
      }).then(res => {
        if (res.status == 200) {
          this.$message({
            type: 'success',
            message: '编辑成功'
          })
        } else {
          this.$message({
            type: 'error',
            message: '编辑失败'
          })
        }

      })
      this.editCard = false
    }
  },
  computed: {
    formDate() {
      let arr = []
      if (this.date[0] && this.date[1]) {
        arr[0] = `${this.date[0].getFullYear()}-${this.date[0].getMonth() + 1}-${this.date[0].getDate()}`
        arr[1] = `${this.date[1].getFullYear()}-${this.date[1].getMonth() + 1}-${this.date[1].getDate()}`
      }
      return arr
    }
  },
  filters: {
    cardStatus(value) {
      if (value == 0) {
        return '未审核'
      } else if (value == 1) {
        return '已审核'
      } else if (value == 2) {
        return '已审核已确认'
      } else if (value == 3) {
        return '驳回'
      }
    }
  },
  mounted() {
    // api_getPunchCard().then(res => {
    //   this.cardList = res.data
    // })

    //date设置为当天
    let today = new Date()


    let today2 = new Date()
    //当天日期-1
    today2.setDate(today2.getDate() - 1)


    this.date = [today2, today]
    this.dateChange()
    console.log(new Date().getTime())

  },
  created() {
    this.getUsers()
    this.getProjects()
  }
}
</script>

<style></style>
